package com.yl.nettylearning.netty.day10;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * @author yangl
 */
public class ClientHandler extends SimpleChannelInboundHandler<Long> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Long msg) throws Exception {
        System.out.println("服务器的IP是：" + ctx.channel().remoteAddress() + "回复的消息是：" + msg);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("client发送数据");
        ctx.writeAndFlush(12345678L );
        /**
         * 如果是字符串类型：
         *      1、该处理器的前一个handler是LongToByteDecoder
         *      2、LongToByteDecoder 的父类是 MessageToByteDecoder
         *      3、父类MessageToByteDecoder
         *      4、如果是long类型，就处理，不是就跳过encode
         */
        ctx.writeAndFlush(Unpooled.copiedBuffer("asdfdghgjhg", CharsetUtil.UTF_8));
    }
}
